package controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.PageRankService;

@SuppressWarnings("serial")
public class CheckPrServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		resp.setHeader( "Cache-Control", "public, max-age=86400");
		resp.setHeader( "Expires", new Date(Calendar.getInstance().getTime().getTime()+24*60*60*1000).toGMTString());
		String name=req.getParameter("url");
		if(name != null)
		{
			PageRankService prService = new PageRankService();
	
			String pr = prService.getPR(URLDecoder.decode(name,"UTF-8"));
			
			req.setAttribute("pr", pr);
			req.setAttribute("name", URLDecoder.decode(name,"UTF-8"));
		}
		try {
			req.getRequestDispatcher("/pr.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
